package com.group18.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group18.entity.AccountUser;
import com.group18.entity.FamilyUser;
import com.group18.entity.MemberUser;
import com.group18.service.AccountUserService;
import com.group18.service.FamilyUserService;
import com.group18.service.MemberUserService;



/**
 * Servlet implementation class DoUpdateMember
 */
@WebServlet("/DoUpdateMember")
public class DoUpdateMember extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoUpdateMember() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		int member_id = Integer.parseInt(request.getParameter("member_id"));
		String name = request.getParameter("family");
		String username = request.getParameter("username_update");
		String password = request.getParameter("password1");
		
		FamilyUser fu = new FamilyUser();
		fu.setName(name);
		FamilyUserService fus = new FamilyUserService();
		int family_id = fus.selectIDByName(fu);
		
		//修改member表的用户信息
		MemberUser mu = new MemberUser();
		mu.setId(member_id);
		mu.setUsername(username);
		mu.setPassword(password);
		mu.setName(name);
		mu.setFamily_id(family_id);
		MemberUserService mus = new MemberUserService();
		int i = mus.upDataMember(mu);
		
		//修改account表中的信息
		AccountUser au = new AccountUser();
		au.setFamily_id(family_id);
		au.setUsername(username);
		au.setMember_id(member_id);
		AccountUserService aus = new AccountUserService();
		int j = aus.updateAccountUsername(au);
		
		String msg = "";
		if(i > 0) {
			msg = "用户信息修改成功";
		}else {
			msg = "用户信息修改失败";
		}
		request.setAttribute("msg", msg);
		
		HttpSession session = request.getSession();
		int id = (int)session.getAttribute("member_id");
		if(id == member_id) {
			session.setAttribute("name", name);
			session.setAttribute("member_id", member_id);
			session.setAttribute("username", username);
			session.setAttribute("family_id", family_id);
			session.setAttribute("password", password);
		}
		
		String jump = request.getParameter("jump");
		String text = request.getParameter("text");
		if(text != null && !text.equals("") && jump.equals("/ToFamilyMember")) {
			request.getRequestDispatcher("/DoSearchMember").forward(request, response);
		}else {
			request.getRequestDispatcher(jump).forward(request, response);
		}
	}

}
